from rest_framework.generics import GenericAPIView, ListAPIView
from rest_framework.mixins import ListModelMixin
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import IsAdminUser,DjangoModelPermissions
from apps.goods.models import SKU, GoodsCategory, SPU, SPUSpecification, Brand
from apps.meiduo_admin.serializers.sku import SKUListSerializer, GoodsCategorySerializer, SPUSimpleSerializer, \
    SPUSpecSerializer
from apps.meiduo_admin.serializers.spu import BrandModelSerializer
from apps.meiduo_admin.utils import PageNum

class SKUModelView(ModelViewSet):
    queryset = SKU.objects.all()
    serializer_class = SKUListSerializer
    pagination_class = PageNum
    permission_classes = [IsAdminUser, DjangoModelPermissions]

"""
1,保存sku需要选择三级分类
2,需要选择spu信息
3,确定了spu，需要根据spu的id查找规格和规格选项信息
"""

class CategoryAPIView(APIView):
    def get(self, request):
        # 获取查询结果集
        cats = GoodsCategory.objects.filter(subs=None)
        # 创建序列化器进行转换
        serializer = GoodsCategorySerializer(cats, many=True)
        # 返回
        return Response(serializer.data)


class SPUSimpleAPIView(ListAPIView):
    queryset = SPU.objects.all()
    serializer_class = SPUSimpleSerializer

class SPUSpecAPIView(APIView):
    def get(self, request, pk):
        queryset = SPUSpecification.objects.filter(spu_id=pk)
        serializer = SPUSpecSerializer(queryset, many=True)
        return Response(serializer.data)






# 获取三级分类使用二级视图和三级视图实现
# class CategoryAPIView(ListModelMixin, GenericAPIView):
#     queryset = GoodsCategory.objects.filter(subs=None)
#     serializer_class = GoodsCategorySerializer
#     def get(self, request):
#         return self.list(request)
#
# class CategoryAPIView(APIView):
#     queryset = GoodsCategory.objects.filter(subs=None)
#     serializer_class = GoodsCategorySerializer